package com.sdgakj.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.List;

public class FeedLikeDaoImpl {

	@PersistenceContext
	private EntityManager em;

	/**
	 * 未删除贴子的所有人的点赞
	 * @param feedId 贴子id
	 * @param pageNumber 
	 * @param pageSize
	 * @return
	 */
    public List<Object[]> findPageLikeLoginName(Long feedId,Integer pageNumber,Integer pageSize){
    	StringBuffer strSql =new StringBuffer();
    	strSql.append(" select u.loginname as loginname,p.create_date as createDate from \n");
    	strSql.append(" (select c1.* from sht_feed_like c1 where 1=1 \n");
		if(feedId != null) {
			strSql.append("  AND c1.feed_id = :feedId \n");
		}
		strSql.append(" order by c1.id asc \n");
		strSql.append("LIMIT :startindex, :rows \n");
		strSql.append(" )p left join sht_user u on c2.user_id=u.id \n");
        Query q = em.createNativeQuery(strSql.toString());
		 if(feedId != null) {
	        	q.setParameter("feedId", feedId);
		 }
		 q.setParameter("startindex", (pageNumber-1) * pageSize);
		 q.setParameter("rows", pageSize);
		return q.getResultList();
    }

    /**
     * 查看帖子的点赞数
     * @param feedId
     * @return
     */
    public Long getTotalCount(Long feedId){
		StringBuffer sql = new StringBuffer();
		sql = new StringBuffer();
		sql.append("SELECT \n");
		sql.append("  COUNT(1) from sht_feed_like s \n");
		sql.append("WHERE 1 = 1 \n");
		if(feedId !=null){
			sql.append(" AND s.feed_id = :feedId");
		}
		Query q = em.createNativeQuery(sql.toString());
		if(feedId !=null){
			q.setParameter("feedId", feedId);
		}
		long total = ((BigInteger) q.getSingleResult()).longValue();
		return total;	
    }
	
}
